package middleware

import (
	"fmt"
	"gin-wheel/global"
	"time"

	"github.com/gin-gonic/gin"
)

func AccessLog() gin.HandlerFunc {
	return func(c *gin.Context) {
		start := time.Now()
		// 处理请求
		c.Next()
		// 结束时间
		end := time.Now()
		//执行时间
		duration := end.Sub(start)

		path := c.Request.URL.Path

		clientIP := c.ClientIP()
		method := c.Request.Method
		statusCode := c.Writer.Status()
		info := fmt.Sprintf("method: %s, path: %s, statusCode: %3d, clientIP: %5s, duration: %s",
			method,
			path,
			statusCode,
			clientIP,
			duration,
		)
		global.ZapLog.Info(info)
	}
}
